const router = require('koa-router')() //导入 koa-router
const mysql = require("../utils/db")  //导入 db.js
const { QUERY_SQL, ADD_SQL, EDIT_SQL, DELETE_SQL } = require("../utils/dbSQL")
router.get('/', async (ctx, next) => {
  await mysql.query(QUERY_SQL).then(async (res) => {
    ctx.body = {
      code: 200,
      data: res,
      message: "查询成功"
    }
  }).catch(() => {
    console.log("查询错误");
  })
})
router.post('/addMysql', async (ctx, next) => {
  let checkData = ctx.request.body
  console.log(checkData);
  await mysql.query(ADD_SQL, checkData).then(async (res) => {
    ctx.body = {
      code: 200,
      message: "添加成功"
    }
  }).catch(e => {
    console.log("添加错误", e);
  })

})
router.post('/editMysql', async (ctx, next) => {
  let checkData = ctx.request.body
  checkData = [{
    name: checkData.name,
    value: checkData.value,
    lable: checkData.lable
  }, checkData.id]
  await mysql.query(EDIT_SQL, checkData).then(async (res) => {
    ctx.body = {
      code: 200,
      message: "编辑成功"
    }
  }).catch(e => {
    console.log("编辑错误", e);
  })

})
router.get('/deleteMysql', async (ctx, next) => {
  let checkData = ctx.request.query
  // checkData = Object.values(checkData)
  checkData = [checkData.id]
  await mysql.query(DELETE_SQL, checkData).then(async (res) => {
    ctx.body = {
      code: 200,
      message: "删除成功"
    }
  }).catch(e => {
    console.log("删除错误", e);
  })

})


module.exports = router

